﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using ASBCC.Database.Entity;
using SourceCode.Workflow.Client;
using ASBCC.Database.Service;

namespace ASBCC.Workflow.K2Manager
{
    public class WithdrawActionHandler : RejectActionHandler
    {
        #region Constructor

        public WithdrawActionHandler(SerializableDictionary dataFields, K2AppForm appForm, K2WorkItem workItem, K2ActionInfo actionInfo, string comment, K2AuditDestination auditDestination, List<K2User> outputDestinationUsers) :
            base(dataFields, appForm, workItem, actionInfo, comment, auditDestination, outputDestinationUsers)
        {
            workItem.ArrvialDate = DateTime.Now;
        }

        #endregion

        #region Override Methods

        /// <summary>
        /// Withdraw动作时,重载图章生成方法,清除所有图章
        /// </summary>
        protected override void BuildProcessStamp()
        {
            K2DataHelper.DisableProcessStamp(WorkItem.ProcInstId);
        }

        /// <summary>
        /// Withdraw动作时,重载Comment生成方法,清除所有Comment
        /// </summary>
        protected override void BuildProcessComment()
        {
            //撤回操作不清楚注释
            //K2DataHelper.DisableProcessComment(WorkItem.ProcInstId);
        }

        protected override void SendMail()
        {
            string actionType = GetActionType();
            string ApplicationNameEn = FlowProcess.ProcDisplayEn;
            string ApplicationNameCn = FlowProcess.ProcDisplayCn;

            if (MyActivityListToBeMoved == null || MyActivityListToBeMoved.Count == 0) return;

            string procName = MyActivityListToBeMoved[0].ProcName;
            string activityName = MyActivityListToBeMoved[0].K2ActivityName;

            Flow_ProcessMailConfigTableEntity mailConfig = K2DataHelper.GetProcessMailConfig(procName, activityName, actionType);
            if (mailConfig == null) return;

            EZVisitApplicationTableEntity entity = EZVisitApplicationManager.SelectByKeys(AppForm.FormId);
            if (entity == null) return;

            foreach (MyActivityTableEntity myActivity in MyActivityListToBeMoved)
            {
                if (myActivity.OwnerID == K2User.TempUser) continue; //临时用户不需要通知

                int toAuditorTemplate = mailConfig.ToAuditorTemplate ?? 0;
                if (toAuditorTemplate <= 0) continue;

                EmployeeTableEntity ownerInfo = K2DataHelper.GetUserInfoById(myActivity.OwnerID ?? 0);

                if (!CheckMailApproverRule(ownerInfo)) continue;

                string[] mailAddressArr = new string[] { ownerInfo.Email };
                NameValueCollection collection = new NameValueCollection();
                collection["{EnglishName}"] = myActivity.OwnerEn;
                collection["{ChineseName}"] = myActivity.OwnerCn;

                if (!entity.EZVisitReqNoIsNull())
                    collection["{SerialNumber}"] = entity.EZVisitReqNo;
                else
                    collection["{SerialNumber}"] = myActivity.ApplicationNo;
                collection["{Applicant}"] = myActivity.ApplicantEn;
                collection["{FormURL}"] = myActivity.FormURL;
                collection["{AuditTime}"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                collection["{Comments}"] = Comment;

                NameValueCollection titleCollection = new NameValueCollection();
                if (!entity.EZVisitReqNoIsNull())
                    titleCollection["{SerialNumber}"] = entity.EZVisitReqNo;
                else
                    titleCollection["{SerialNumber}"] = myActivity.ApplicationNo;

                MailService.MailSender(mailAddressArr, toAuditorTemplate, collection, titleCollection);
            }
        }

        protected override string GetActionType()
        {
            return K2ActionType.Withdraw;
        }

        #endregion
    }
}
